package cn.tedu.charging.device.dao.repository.impl;

import cn.tedu.charging.device.dao.repository.AbstractStationCacheRepository;
import cn.tedu.charging.device.dao.repository.StationCacheRepository;
import cn.tedu.charging.device.pojo.po.StationCanalPO;
import cn.tedu.charging.device.pojo.po.StationPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  * 共享 所有的站 一个map
 *  * Map<BigKey,Map<hashKey,value>>
 *  *
 *  * BigKey  stations_hash
 *  * hashKey 站的id  1
 *  * value   站的详情  stationPo
 *
 *  和 String
 *  *
 *  * Map<BigKey,Map<hashKey,value>> bigmap = map();
 *  * Map<hashKey,value> smallmap =map();
 *  *
 *  * smallmap.put("1","stationPo1")
 *  * smallmap.put("2","stationPo2")
 *  * smallmap.put("3","stationPo3")
 *  *
 *  * bigmap.put(stations_hash,smallmap)
 *
 *
 */
@Slf4j
@Repository("GeoAndShareHash")
public class StationCacheGeoAndShareHashRepositoryImpl
        extends AbstractStationCacheRepository
        implements StationCacheRepository {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void saveAllStations(List<StationPO> stations) {
        saveLocationByGEOAndMemberIsId(stations);
        saveStationInfo2ShareHash(stations);
    }

    private void saveStationInfo2ShareHash(List<StationPO> stations) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        String bigKey = "station_info_hash_share";
        Map<String,StationPO> smallMap = new HashMap<>();
        for (StationPO stationPO : stations) {
            String hashKey = String.valueOf(stationPO.getId());
            smallMap.put(hashKey,stationPO);
        }
        hashOperations.putAll(bigKey,smallMap);
    }

    @Override
    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    @Override
    public List<StationPO> search(Double lng, Double lat, Integer distance) {
        return super.search(lng, lat, distance);
    }

    @Override
    public void insert(StationCanalPO stationCanalPO) {
        //todo
    }

    @Override
    public void update(StationCanalPO before, StationCanalPO after) {
        //todo
    }

    @Override
    public void delete(StationCanalPO stationCanalPO) {
        //todo
    }


    @Override
    public StationPO getStationById(Integer stationId) {
        HashOperations<String,String,StationPO> hashOperations = redisTemplate.opsForHash();
        String bigKey = "station_info_hash_share";
        return hashOperations.get(bigKey, String.valueOf(stationId));
    }
}
